﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
#if (LINQ)
using System.Linq;
#endif
using System.Text;

using Robin.LightNing.Interfaces;

namespace Robin.LightNing.Core
{
    /// <summary>
    /// 集合排序的时候使用
    /// </summary>
    /// <typeparam name="T">需要比较的实体类型</typeparam>
    public class rbEntityComparer<T> : IComparer<T>
        where T : rbEntity
    {
        public rbEntityComparer(PropertyDescriptor sortProperty, ListSortDirection sortDirection)
        {
            this.sortProperty = sortProperty;
            this.sortDirection = sortDirection;
        }

        #region IComparer<T> Members

        int IComparer<T>.Compare(T obj1, T obj2)
        {
            rbEntity obj = obj1 as rbEntity;
            rbColumnMetadata esColumn = obj.rb.Meta.Columns.FindByPropertyName(sortProperty.Name);
            return obj1.OnSort(obj2, esColumn, sortProperty.Name);
        }

        #endregion

        private PropertyDescriptor sortProperty;
        private ListSortDirection sortDirection;
    }
}

